﻿var mxPlugin = window.mxPlugin || {};

$(document).ready(function () {
    mxPlugin.dynamic = {
        init: function () {
            mxPlugin.dynamic.reloadAll();
            mxPlugin.dynamic.rebindAll();
            mxPlugin.dynamic.setWidgets();
        },

        reloadAll: function () {
            $("[mx-src]").each(function (i, e) {
                mxPlugin.dynamic.reload(e);
            });
        },

        rebindAll: function () {
            $("[mx-route]").each(function (i, e) {
                mxPlugin.dynamic.rebind(e);
            });
        },

        setWidgets: function (element) {
            if (element) {
                $("[mxInput]", element).each(function (i, e) {
                    mxPlugin.dynamic.setWidgetsOnElement(e);
                });
            } else {
                $("[mxInput]").each(function (i, e) {
                    mxPlugin.dynamic.setWidgetsOnElement(e);
                });
            }
        },

        reload: function (element) {
            var url = $(element).attr("mx-src");

            $.ajax({
                url: url,
                type: "get",
                cache: false,
                success: function (r, s) {
                    $(element).html(r);
                    mxPlugin.dynamic.rebind(element);
                }
            });
        },

        setWidgetsOnElement: function (element) {
            var descriptor = $(element).attr("mxInput");
            switch (descriptor) {
                case "date":
                    $(element).datepicker();
                    break;
                case "autocomplete":
                    $(element).autocomplete({
                        minLength: 2,
                        source: function (request, response) {
                            var term = request.term;
                            var cache = $(element).data("mx-autocompleteCache") || {};
                            if (term in cache) {
                                response(cache[term]);
                                return;
                            }

                            var url = $(element).attr("mxInputSource") + "?" + $(element).attr("mxInputField") + "=" + term;
                            $.ajax({
                                url: url,
                                type: "get",
                                cache: true,
                                success: function (s, r) {
                                    cache[term] = s;
                                    response(s);
                                    return;
                                }
                            });
                        }

                    });
                    break;
            }
        },

        rebind: function (element) {
            var descriptor = $(element).attr("mx-route");
            descriptor = $.parseJSON(descriptor);

            $(descriptor).each(function (a, b) {
                if (!b.selector)
                    b.selector = "a,:button";

                $(b.selector, element).click(function (c, d) {
                    eval(b.command + "(this, b)");
                    return false;
                });

                if ($(element).is(b.selector)) {
                    $(element).click(function (c, d) {
                        eval(b.command + "(this, b)");
                        return false;
                    });
                }
            });

            mxPlugin.dynamic.setWidgets(element);
        },

        retarget: function (source, descriptor) {
            var target = $(descriptor.target);
            var url = source.href;

            if (url == "") {
                url = target.attr("mx-src");
            } else {
                target.attr("mx-src", url);
            }

            if (url == null) {
                target.html("");
            } else {
                $.ajax({
                    url: url,
                    type: "get",
                    cache: false,
                    success: function (r, s) {
                        target.html(r);
                        mxPlugin.dynamic.rebind(target);
                    }
                });
            };
        },

        dialog: function (source, descriptor) {
            var target = null;
            if (descriptor.target) {
                target = $(descriptor.target);
            }
            else {
                target = $("[mx-dialogTarget]");
            }
            var url = source.href;

            if (url == "") {
                url = target.attr("mx-src");
            }

            if (descriptor.action) {
                eval("url=" + descriptor.action + "(\"" + url + "\") || url");
            }

            target.attr("mx-src", url);

            if (url == null) {
                target.html("");
            } else {
                $.ajax({
                    url: url,
                    type: "get",
                    cache: false,
                    success: function (r, s) {
                        target.html(r);
                        mxPlugin.dynamic.setWidgets(target);
                        $("input[type='submit']", target).click(function () {
                            target.removeAttr("mx-src");
                            if (descriptor.submit) {
                                eval(descriptor.submit + "(target, descriptor)");
                            } else {
                                var callback = null;
                                if (descriptor.callback)
                                    callback = eval(descriptor.callback);
                                mxPlugin.async.postDialog(target, descriptor, callback);
                            }
                            target.dialog("close");
                            return false;
                        });
                        target.dialog({
                            title: $(source).attr("title"),
                            modal: true,
                            width: descriptor.width
                        });
                    }
                });
            };
        }
    };

    mxPlugin.dynamic.init();
});